//
//  GThridLevelView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-7-27.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GThridLevelView.h"
#import "mcf.h"
#import "GSectionView.h"

@interface GThridLevelView ()
{
    UIScrollView * contentScrollView_;
    
    UIButton * closeButton_;
    GSectionView * sectionView_;
}


@property (nonatomic, strong) UIScrollView * contentScrollView;
@property (nonatomic, strong) UIButton * closeButton;

@property (nonatomic, strong) GSectionView * sectionView;

- (void) privateInit;

@end

@implementation GThridLevelView

@synthesize section = section_;
@synthesize contentScrollView = contentScrollView_;

@synthesize delegate = delegate_;
@synthesize closeButton = closeButton_;

@synthesize sectionView = sectionView_;

- (void) dealloc
{
    YRelease(section_)
    YRelease(contentScrollView_)
    
    YRelease(closeButton_)
    YRelease(sectionView_)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self) 
    {
        [self privateInit];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}


- (void) privateInit
{
    if (self.bInited) 
    {
        return;
    }
    
    [self setBackgroundColor:RGB(74, 82, 89)];
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self addSubview:self.closeButton];
    BUTTON_FUNCTION(self.closeButton, closeSelf)
    
    
    UIScrollView * temp_scrollview = [[UIScrollView alloc] init];
    self.contentScrollView = temp_scrollview;
    YRelease(temp_scrollview)
    
    [self addSubview:self.contentScrollView];
    
    GSectionView * temp_sectionView = [[GSectionView alloc] init];
    self.sectionView = temp_sectionView;
    YRelease(temp_sectionView)
    
    [self.contentScrollView addSubview:self.sectionView];
    
    
    [self bringSubviewToFront:self.closeButton];
    [self reAdjustLayout];
    self.bInited = TRUE;
}


- (void) reAdjustLayout
{
    [self.contentScrollView setFrame:CGRectMake(0, isPad ? 46 : 15, self.width, self.height - (isPad ? 46 : 15))];
    [self.closeButton setFrame:CGRectMake(5, 5, isPad ? 40 : 30, isPad ? 40 : 30)];
    
    float needHeight = [self.sectionView needHeight];
    [self.sectionView setFrame:CGRectMake(0, 45, self.width, needHeight)];
    [self.contentScrollView setContentSize:CGSizeMake(self.width, needHeight + (isPad ? 47 : 15 ))];
}





- (void) closeSelf
{
    DELEGATE_CALLBACK(exitThridLevelView:, nil)
}


- (void) setSection:(GSection *)sectionValue
{
    YRelease(section_)
    section_ = [sectionValue retain];
    
    if (!section_) {
        return;
    }
    
    [self.sectionView setSection:section_];
//    [self reAdjustLayout];
    
    [self performSelector:@selector(reAdjustLayout) withObject:nil afterDelay:0.1f];
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
}

@end
